

package edu.towson.cosc.cosc455.biedermann.compiler;

import edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer;

public class MyLexicalAnalyzer implements LexicalAnalyzer {

	//place holder variables:
	public static String tagReturn = "";
	public static char[] currInputTxt;
	public static char charReturn;
	public static int currentPosition;
	public static String text= "";
	
	/**
	 * Method parses through file by calling getCharacter()
	 * checks characters against 3 cases
	 * Case 1: character is a space, get next character
	 * Case 2: character is a "#", send to processTag()
	 * Case 3: character is regular text 
	 * @see edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer#getNextToken()
	 */
	@Override
	public void getNextToken() {
		char c;
		getCharacter();
		c = charReturn;
		
		//checks cases
		//case 1: character is a string. Ignore, get next char
		while(c==' '){
			getCharacter();
			c = charReturn;
		}
		
		//case 2: character is a #, then sends to ____ method to get name of tag
		if (c=='#'){
			processTag();  
		}
		
		//case 3: regular text that will only be stored as is
		else {
			text +=c;
			processTxt();
			currentPosition --;
			MyCompiler.parsedInput.add(text);
			MyCompiler.token = text;
			LexicalTokens.textList.add(text);
			text = "";
		}
	}

	/**
	 * Method to parse through file stepping through one character at a time
	 * @see edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer#getCharacter()
	 */
	@Override
	public void getCharacter() {
		currInputTxt = MyCompiler.inputTxt.toCharArray();
		if(currentPosition < currInputTxt.length){
			charReturn = currInputTxt[currentPosition];
			currentPosition++;
		}
	}

	@Override
	public void addCharacter() {}

	/**
	 * Method to see if token found by processTag() is a legal token
	 * @see edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer#lookupToken(java.lang.String)
	 */
	@Override
	public boolean lookupToken(String x) {

		if (LexicalTokens.lexicalTokenList.contains(x.toUpperCase())){
			return true;
		}
		else{
			return false;
		}
	}

	@Override
	public boolean isSpace(String c) {
		return false;
	}
	
	/**
	 * Method that collects text after finding a "#" and checks against three cases
	 * Case 1: collects text until first space, sends to lookupTag(), if valid, returns token
	 * Case 2: collects text until second space, sends to lookupTag(), if valid, returns token
	 * Case 3: collects text until third space, sends to lookupTag(), if valid, returns token
	 * if no token is found, reports error
	 * @see edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer#processTag()
	 */
	public void processTag(){
		String tokenReturn = "#";
		char c;
		getCharacter();
		c= charReturn;
		while(c !=' '){
			tokenReturn += c;
			getCharacter();
			c= charReturn;
		}
		//check to see if token has been found
		if(lookupToken(tokenReturn)){
			MyCompiler.token = tokenReturn;
			MyCompiler.parsedInput.add(tokenReturn.toUpperCase());
			return;
		}
		
		getCharacter();
		c= charReturn;
		tokenReturn+= ' ';
		while(c!=' '){
			tokenReturn += c;
			getCharacter();
			c= charReturn;
		}
			//check to see if token has been found
			if(lookupToken(tokenReturn)){
				MyCompiler.token = tokenReturn;
				MyCompiler.parsedInput.add(tokenReturn.toUpperCase());
				return;				
			}
			
			getCharacter();
			c= charReturn;
			tokenReturn+= ' ';
			while(c!=' '){
				tokenReturn += c;
				getCharacter();
				c= charReturn;
			}
			
			//check to see if token has been found
			if (lookupToken(tokenReturn)){
				MyCompiler.token = tokenReturn;
				MyCompiler.parsedInput.add(tokenReturn.toUpperCase());
				return;
				}
			//No token found, error
			else{
				System.err.println("Lexical Error: "+ "\""+tokenReturn +"\""+" is not a valid token.");
				System.exit(0);
				}
			}
	
	/**
	 * Method to collect regular text until "#" is found
	 * @see edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer#processTxt()
	 */
	public void processTxt(){
		char c;
		getCharacter();
		c= charReturn;
		while (c!='#'){
			text +=c;
			getCharacter();
			c= charReturn;
		}		
	}
}
